package com.example.biyesheji.service.impl;

import com.example.biyesheji.mapper.ProductMapper;
import com.example.biyesheji.model.param.ThreeMonthSales;
import com.example.biyesheji.model.result.TrendAnalze;
import com.example.biyesheji.pojo.ProductSales;
import com.example.biyesheji.service.ThreeMonthSaleService;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ThreeMonthSaleServiceImpl implements ThreeMonthSaleService {

    @Autowired
    private ProductMapper productMapper;
    /**
     * @Params
     * @Return @return {@link List }<{@link ThreeMonthSales }>
     * @Description 获取用户的消费情况
     * @Throws
     * @Author xwh
     * @Date 2023/02/16
     */
    @Override
    public ThreeMonthSales getSaleList(String userid) {
        //统计用户消费记录的
        List<String> brandList = new ArrayList<>();
        List<String> monthList = new ArrayList<>();
        List<Integer> saleList = new ArrayList<>();

        //查询单个用户信息
        List<ProductSales> byUserId = productMapper.getByUserId(userid);

        for (ProductSales p:byUserId
             ) {
            if (p.getBrand() ==null || p.getBrand() ==""){continue;}
            else {
                brandList.add(p.getBrand());
                monthList.add(p.getEventTime().substring(5,7)+"月");
                saleList.add(Math.round(p.getPrice()));
            }

        }
        ThreeMonthSales threeMonthSales = new ThreeMonthSales();
        threeMonthSales.setSaleList(saleList);
        threeMonthSales.setMonthList(monthList);
        threeMonthSales.setBrandList(brandList);

        return threeMonthSales;
    }

    @Override
    public TrendAnalze getMoreInfo(String tmp) {
        List<String> brandList = new ArrayList<>();
        List<String> monthList = new ArrayList<>(Arrays.asList("1季度","2季度","3季度","4季度"));
        List<ArrayList> dataList = new ArrayList<>();

        //个季度数据列表
        ArrayList one = new ArrayList();
        ArrayList two = new ArrayList();
        ArrayList three = new ArrayList();
        ArrayList four = new ArrayList();
        List<ProductSales> productSales = productMapper.getmoreInfo(tmp);
        for (ProductSales p:productSales){
            brandList.add(p.getBrand());

            int i = Integer.parseInt(p.getEventTime().substring(5, 7));
            if ( i== 1 ||i ==2 || i ==3){
                one.add(p.getPrice());
            }
            if (i== 4 ||i ==5 || i ==6){
                two.add(p.getPrice());
            }
            if (i== 7 ||i ==8 || i ==9){
                three.add(p.getPrice());
            }
            if (i== 10 ||i ==11 || i ==12){
                four.add(p.getPrice());
            }

        }
        dataList.add(one);dataList.add(two);dataList.add(three);dataList.add(four);
        TrendAnalze trendAnalze =new TrendAnalze();
        trendAnalze.setBrandList(brandList.stream().distinct().collect(Collectors.toList()));
        trendAnalze.setDataList(dataList);
        trendAnalze.setMonthList(monthList);



        return trendAnalze;
    }
}
